var Account = {}

Account.init = function(searchUserType){
	Account.initGrid(searchUserType);
}

Account.initGrid = function(type){
	var url = context+'/manager/account!';
	var title ='';
	switch(type){
	case 0:url = url + 'adminUserData.action';
		var columns = [
						{field:'userName',title:'用户名',width:150,align:'center'},
						{field:'phone',title:'联系电话',width:170,align:'center'},
						{field:'email',title:'邮箱地址',width:216,align:'center'},
						{field:'id',title:'操作',width:200,align:'center',formatter:function(value,rec){
							return "<a href='#' onclick='Account.userDetail("+value+")' style='color:blue'>查看</a> ";
						}}
					]
		break;
	case 1:url = url + 'operatorUserData.action';
	var columns = [
					{field:'userName',title:'用户名',width:150,align:'center'},
					{field:'phone',title:'联系电话',width:170,align:'center'},
					{field:'email',title:'邮箱地址',width:216,align:'center'},
					{field:'id',title:'操作',width:200,align:'center',formatter:function(value,rec){
						return "<a href='#' onclick='Account.userDetail("+value+")' style='color:blue'>查看</a> | <a href='#' onclick='Account.changePwd("+value+",\""+rec.userName+"\")' style='color:blue'>修改密码</a> | <a href='#' onclick='Account.deleteUser("+value+")'  style='color:blue'>删除</a>";
					}}
				]
		break;
	case 2:url = url + 'commonUserData.action';
	var columns = [
					{field:'userName',title:'用户名',width:140,align:'center'},
					{field:'phone',title:'联系电话',width:140,align:'center'},
					{field:'email',title:'邮箱地址',width:160,align:'center'},
					{field:'point',title:'积分',width:120,align:'center'},
					{field:'id',title:'操作',width:175,align:'center',formatter:function(value,rec){
						return "<a href='#' onclick='Account.userDetail("+value+")' style='color:blue'>查看</a> | <a href='#' onclick='Account.changePwd("+value+",\""+rec.userName+"\")' style='color:blue'>修改密码</a> | <a href='#' onclick='Account.deleteUser("+value+")'  style='color:blue'>删除</a>";
					}}
				]
		break;
	case 3:url = url + 'platerUserData.action';
	var columns = [
					{field:'userName',title:'用户名',width:140,align:'center'},
					{field:'phone',title:'联系电话',width:140,align:'center'},
					{field:'email',title:'邮箱地址',width:160,align:'center'},
					{field:'point',title:'积分',width:120,align:'center'},
					{field:'id',title:'操作',width:175,align:'center',formatter:function(value,rec){
						return "<a href='#' onclick='Account.userDetail("+value+")' style='color:blue'>查看</a> | <a href='#' onclick='Account.changePwd("+value+",\""+rec.userName+"\")' style='color:blue'>修改密码</a> | <a href='#' onclick='Account.deleteUser("+value+")'  style='color:blue'>删除</a>";
					}}
				]
		break;
	default : return false;
	}
	$('#userListTable').datagrid({
		width:768,
		height:'auto',
		striped: true,
		url:url,
		remoteSort: false,
		idField:'id',
		columns:[columns],
		pagination:true,
		loadMsg:'正在加载数据,请等待...',
		rownumbers:true
	});
	var p = $('#userListTable').datagrid('getPager');
	if (p){
		$(p).pagination({
			showPageList:false,
			beforePageText:'第',
			afterPageText:"页，共<font color='blue'> {pages} </font>页",
			displayMsg:"当前显示 <font color='red'>{from}</font> 到 <font color='red'>{to}</font> 条记录，共<font color='red'> {total} </font>条记录",
		});
	}
}

Account.deleteUser = function(userId){
	$.messager.defaults={ok:"确定",cancel:"取消"};
	$.messager.confirm('', '确定删除吗？', function(r){
		if (r){
			$.ajax({
				type:"POST",
				url:context+'/manager/account!deleteUser.action', 
				data:{
					'userId':userId
				},
				success:function(text){
					$('#userListTable').datagrid('reload');
				}
			});
		}
	});
}

Account.userDetail = function(userId){
	$('#allInfo_dialog').dialog({
		width:400,
		height:'auto',
		title:'用户详细信息',
		onClose:function(){
			
		}
	});
	$.ajax({
		type:"POST",
		url:context+'/manager/account!userDetail.action?userId='+userId, 
		success:function(text){
			Account.initUserDetailData(text);
			$('#allInfo_dialog').dialog("open");
		}
	});
	
//	location.href = context+'/manager/account!userDetail.action?userId='+userId;
}

Account.initUserDetailData = function(user){
	$('#_userName').text(user.name);
}

Account.changePwd = function(userId,userName){
	$('#userId').val(userId);
	$('#userName').text(userName);
	$('#pwd_dialog').dialog({
		width:400,
		height:'auto',
		title:'密码修改',
		onClose:function(){
			
		}
	});
	$('#pwd_dialog').dialog("open");
}

Account.checkPwd = function(){
	if($('#pwd_new').val()!=$('#pwd_ck').val()){
		$('#errorMsg').text('确认密码与新密码不一致，请重新输入！');
		$('#pwd_new').val('');$('#pwd_ck').val('');
		return false;
	}else{
		$('#errorMsg').text('');
		return true;
	}
}

Account.sureChange = function(){
	if(!util.checkFormFields($('#pwd_form'))){
		alert("请按提示输入合法内容，或完成必填项！");
		return false;
	}else{
		Account.checkPwd();
	}
	$.ajax({
		type:"POST",
		url:context+'/manager/account!modPwd.action', 
		data:{
			'userId':$('#userId').val(),
			'pwd_old':$('#pwd_old').val(),
			'pwd_new':$('#pwd_new').val()
		},
		success:function(text){
			alert(text);
		}
	});
}

Account.addUser = function(){
	
	var fileName = $("#uploadFile").val();
	if (fileName == '') {
		alert("上传文件不能为空,请选择文件,继续上传!");
		return 0;
	}

	var fileNameArray = fileName.split("\\");
	var imageOriginalName = fileNameArray[fileNameArray.length-1];
	$("#fileName").attr("value",imageOriginalName);
	
	
	if(!util.checkFormFields($('#user_form'))){
		alert("请按提示输入合法内容，或完成必填项！");
		return false;
	}else{
		if(!Account.checkPwd())
			return false;
		else{
			
			var myForm = $("#user_form");
			myForm.attr("action",context+'/manager/account!addUser.action');
			myForm.submit();
			
//			$.ajax({
//				type:"POST",
//				url:context+'/manager/account!addUser.action?'+$('#user_form').serialize(), 
//				success:function(text){
//					alert(text);
//				}
//			})
		}
	}
}

Account.uploadImage = function(){
	
	var url = context+"/manager/fileupload!imageUpload.action";
	
	 var options = { 
		        beforeSubmit : function(){
		        	var fileName = $("#uploadFile").val();
		        	alert(fileName)	;
		        	if (fileName == '') {
		        		alert("上传文件不能为空,请选择文件,继续上传!");
		        		return false;
		        	}

		        	var fileNameArray = fileName.split("\\");
		        	var imageOriginalName = fileNameArray[fileNameArray.length-1];
		        	$("#fileName").attr("value",imageOriginalName);
		        	
		        },  // pre-submit callback 
		        success : function(){
					alert("ddd");
					alert(data.photoUrl);
//					$('#photoUrl').val(data.photoUrl);
		        },  // post-submit callback 
		 
		        // other available options: 
		        url : url         // override for form's 'action' attribute 
		        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
		        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
		        //clearForm: true        // clear all form fields after successful submit 
		        //resetForm: true        // reset the form after successful submit 
		 
		        // $.ajax options can be used here too, for example: 
		        //timeout:   3000 
		    }; 
		 
		    // bind form using 'ajaxForm' 
	 	$('#uploadImage').ajaxForm(options); 
//	 $('#uploadImage').submit(function() { 
//	        // inside event callbacks 'this' is the DOM element so we first 
//	        // wrap it in a jQuery object and then invoke ajaxSubmit 
//	        $(this).ajaxSubmit(options); 
//	 
//	        // !!! Important !!! 
//	        // always return false to prevent standard browser submit and page navigation 
//	        return false; 
//	    }); 
	
	
	
//	$('#uploadImage').form('submit', {
//		url:url,
//		onSubmit: function(){
//          // do some check
//          // return false to prevent submit;
//		},
//		success : function(data) {
//			alert("ddd");
//			//alert(data.msg);
//			$('#photoUrl').val(data.photoUrl);
//		}
//	});
}


Account.test = function(){
	alert(3);
	alert($('#uploadImage').attr('action'));
	
	
	$('#uploadImage').ajaxSubmit({  //js构建form表单，同时使用ajax提交
	    beforeSubmit:function(){
	    	var fileName = $("#uploadFile").val();
//        	alert(fileName)	;
        	if (fileName == '') {
        		alert("上传文件不能为空,请选择文件,继续上传!");
        		return false;
        	}

        	var fileNameArray = fileName.split("\\");
        	var imageOriginalName = fileNameArray[fileNameArray.length-1];
        	$("#fileName").attr("value",imageOriginalName);
	    },
	     success: function(data) { //表单提交成功
	       //do something ... ...
//	      $('#avatar_tip').html('');
//	      $('#avatar_box').unwrap();  //去除表单
	    }
	});
}
	

